package domain.service;


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.Charset;

import org.jsoup.Jsoup;
import org.jsoup.nodes.*;
import org.jsoup.select.*;

import domain.data.QnAPersistenceManager;
import domain.model.Domain;
import domain.model.Keyword;

import java.util.*;

import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;


public class AlexaUpdate {
	public AlexaUpdate(String name_domain)
	{
		try {
			PersistenceManager psm = QnAPersistenceManager.get().getPersistenceManager();
			URL dataURL = new URL("http://www.alexa.com/siteinfo/"+name_domain);
	
			BufferedReader reader = new BufferedReader(new InputStreamReader(dataURL.openStream(), Charset.forName("UTF8")));
			String content = "";
			while(1==1)
			{
				String str = reader.readLine();
				if(str==null) break;
				content+=str;
			}
	
			Document doc = Jsoup.parse(content);

			// visitor
			String visit = "0";
			if(doc.select(".visitors_percent").select(".avg").size() > 0)
			{
				visit = doc.select(".visitors_percent").select(".avg").last().text().trim();
			}
			// rank
			String ranks = null;
			if(doc.select("#siteStats").select("div").size() > 0)
			{
				ranks = doc.select("#siteStats").select("div").first().text().trim().replace(",","");
			}
			int rank = 0;
			if(ranks != null && !ranks.equals(""))
			{
				rank=Integer.parseInt( ranks );
			}
			
			
			//keywords
			ArrayList<String> keywords = new ArrayList<String> ();
			Elements elements = doc.select("#top-keywords-from-search");
			PersistenceManager psms = QnAPersistenceManager.get().getPersistenceManager();
			if(elements.size() > 0)
			{
				for (Element element : elements.first().select("u")) {
					keywords.add(element.text().trim());
					
					Query query1 = psms.newQuery(Keyword.class);
					query1.setFilter("name=='"+element.text().trim()+"' && domain=='"+name_domain+"'");
					@SuppressWarnings("unchecked")
					List<Keyword> check_key = (List<Keyword>) query1.execute();
					if(check_key.size() <= 0)
					{
						Keyword insert_key = new Keyword();
						insert_key.setName(element.text().trim());
						insert_key.setDomain(name_domain);
						psms.makePersistent(insert_key);
					}
				}
			}
			
			@SuppressWarnings("unchecked")
			List<Domain> domainList=  (List<Domain>)psm.newQuery(Domain.class,"name == '"+name_domain+"'").execute();
			if(domainList.size()>0)
			{
				Domain domain = domainList.get(0);
				
				psm=JDOHelper.getPersistenceManager(domain);
				domain.setLastUpdateDate(new Date());
				domain.setRankAlexa(rank);
				domain.setPopularKeywords(keywords);
				domain.setVisit(visit);
				domain.setSpeed(doc.select(".speedAd").select("p").first().text().replaceAll("[^(]+[(]+", "").replaceAll("[)]+[\\w\\W]+", ""));
				
				psm.currentTransaction().begin();
				psm.makePersistent(domain);
				psm.currentTransaction().commit();				
			}
		} catch (Exception e) {
			
			e.printStackTrace();
		}
	}
//	public static void main( String args[])
//	{
//		
//		Domain domain = new Domain();
//		domain.setName("google.com");
//		new AlexaUpdate(domain);
//	}
	
}
